StatelessWidget vs StatefulWidget

  • Code

    1. StatelessWidget

    1. Immutable: Once created, a StatelessWidget cannot change its properties or behavior.

    2. No internal state: Stateless widgets don't have any mutable state. They rely only on the information provided in their constructor and can't change during the widget's lifetime.

    3. Lightweight: Since they don’t hold any mutable state, they are lightweight and simple to use.

    4. Example use cases: Displaying static content like text, icons, or containers that don't need to change dynamically.

    
                     import 'package:flutter/material.dart';
    
                      class MyStatelessWidget extends StatelessWidget {
                        @override
                        Widget build(BuildContext context) {
                          return Container(
                            child: Text('This is a Stateless Widget'),
                          );
                        }
                      }
    
    
                      

    2. StatefulWidget

    1. Mutable state: StatefulWidget can hold mutable state that can change during the lifetime of the widget.

    2. State management: These widgets are associated with a separate State object that can change over time in response to various factors like user actions, data changes, etc.

    3. Dynamic updates: Ideal for UI components that need to update their appearance or behavior dynamically, such as handling user input, animations, etc.

    
    
                      import 'package:flutter/material.dart';
    
                      class MyStatefulWidget extends StatefulWidget {
                        @override
                        _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
                      }
    
                      class _MyStatefulWidgetState extends State<MyStatefulWidget> {
                        int _counter = 0;
    
                        void _incrementCounter() {
                          setState(() {
                            _counter++;
                          });
                        }
    
                        @override
                        Widget build(BuildContext context) {
                          return Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Text('Counter: $_counter'),
                                ElevatedButton(
                                  onPressed: _incrementCounter,
                                  child: Text('Increment'),
                                ),
                              ],
                            ),
                          );
                        }
                      }
    
    
    
    If the widget doesn't require changes in its state, you can use a StatelessWidget. If the widget needs to manage and update its state, then a StatefulWidget is the appropriate choice.